import mysql.connector
from dotenv import load_dotenv
import os

# 加载 .env 配置
load_dotenv()

# 从 .env 文件中获取数据库连接信息
DB_HOST = os.getenv('DB_HOST')
DB_USER = os.getenv('DB_USER')
DB_PASSWORD = os.getenv('DB_PASSWORD')
DB_NAME = os.getenv('DB_NAME')

class DatabaseConnection:
    """数据库连接管理类"""
    
    @staticmethod
    def get_connection():
        """获取数据库连接"""
        try:
            # 创建并返回数据库连接
            connection = mysql.connector.connect(
                host=DB_HOST,
                user=DB_USER,
                password=DB_PASSWORD,
                database=DB_NAME
            )
            return connection
        except mysql.connector.Error as err:
            print(f"Error: {err}")
            return None

    @staticmethod
    def close_connection(connection):
        """关闭数据库连接"""
        if connection:
            connection.close()
